﻿using System;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Presentation
{
    public partial class LogOn : System.Web.UI.Page
    {
        private UserBusiness _userBusiness;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _userBusiness = UserBusiness.RetrieveInstance();
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckAuthenticate();
            }
        }

        protected void LogOnButton_Click(object sender, EventArgs e)
        {
            try
            {
                string username = UserNameTextBox.Text;
                string password = PasswordTextBox.Text;
                if (!_userBusiness.IsExistUserName(username))
                {
                    StatusLabel.Text = "User not found";
                    return;
                }
                if (!_userBusiness.IsActiveUserName(username))
                {
                    StatusLabel.Text = "User is not actived";
                    return;
                }
                User user = _userBusiness.AuthenticateUser(username, password);
                if (user == null)
                {
                    StatusLabel.Text = "Password is incorrect";
                    return;
                }
                Session["user"] = user;
                CheckAuthenticate();
            }
            catch (MockException exception)
            {
                StatusLabel.Text = exception.ExceptionMessage;
            }
        }

        private void CheckAuthenticate()
        {
            try
            {
                if (Session["user"] != null)
                {
                    User user = (User)Session["user"];
                    if (user.Role.RoleName == "Contractor")
                    {
                        if (!UserBusiness.CheckValidEmail(user.Email))
                        {
                            Response.Redirect(String.Format(CultureInfo.CurrentCulture, "UserProfile.aspx?uname={0}&rname={1}&edit=1", user.UserName, user.Role.RoleName));
                        }
                        Response.Redirect("ListTimesheet.aspx");
                    }
                    else if (user.Role.RoleName == "Client")
                    {
                        if (!UserBusiness.CheckValidEmail(user.Email))
                        {
                            Response.Redirect(String.Format(CultureInfo.CurrentCulture, "UserProfile.aspx?uname={0}&rname={1}&edit=1", user.UserName, user.Role.RoleName));
                        }
                        Response.Redirect("ApproveTimesheet.aspx");
                    }
                    else if (user.Role.RoleName == "Consultant")
                    {
                        Response.Redirect("ViewContracts.aspx");
                    }
                    else if (user.Role.RoleName == "Admin")
                    {
                        Response.Redirect("Admin/AddContract.aspx");
                    }
                    else
                    {
                        StatusLabel.Text = "Can't log on to system";
                    }
                }
            }
            catch (ArgumentException exception)
            {
                StatusLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                StatusLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                StatusLabel.Text = exception.ExceptionMessage;
            }
        }
    }
}